# -*- coding: utf-8 -*-


from PyQt5.QtWidgets import *
from PyQt5.Qt import *
import sys


class Check(QWidget):
    def __init__(self):
        super(Check, self).__init__()

        Box = QGroupBox()   # 容器，分隔线
        Box.setFlat(True)

        layout = QHBoxLayout()  # 水平布局

        self.box1 = QCheckBox("&box1")  # 多选框
        self.box1.setChecked(True)
        # stateChanged信号一般在复选框的状态发生改变时发射
        self.box1.stateChanged.connect(lambda: self.btn(self.box1))
        layout.addWidget(self.box1)

        self.box2 = QCheckBox("&box2")
        self.box2.toggled.connect(lambda: self.btn(self.box2))
        layout.addWidget(self.box2)

        self.box3 = QCheckBox("&box3")
        self.box3.setTristate(True)     # 开启三种模式
        self.box3.setCheckState(Qt.PartiallyChecked)    # 半选中状态
        self.box3.stateChanged.connect(lambda: self.btn(self.box3))
        layout.addWidget(self.box3)

        Box.setLayout(layout)   # 将布局器放入容器
        mainLayout = QVBoxLayout()
        mainLayout.addWidget(Box)

        self.setLayout(mainLayout)

    def btn(self, box):
        # isChecked判断是否选中
        chk1 = self.box1.text() + ", box1= " + str(self.box1.isChecked()) + ',chk1State= ' + str(
            self.box1.checkState()) + '\n'
        chk2 = self.box2.text() + ", box1= " + str(self.box2.isChecked()) + ',chk1State= ' + str(
            self.box3.checkState()) + '\n'
        chk3 = self.box3.text() + ", box1= " + str(self.box3.isChecked()) + ',chk1State= ' + str(
            self.box3.checkState()) + '\n'
        print(chk1, chk2, chk3)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    check = Check().show()
    sys.exit(app.exec_())
